package com.example.cola.common.dto;

import com.example.cola.common.error.ErrorCode;

import java.io.Serializable;

/**
 * @author fenglin
 * @date 2022/8/13
 */
public class CommonResult<T> implements Serializable {

	private static final long serialVersionUID = -4569956326959045094L;

	private T data;
	private boolean success;
	private String code;
	private String msg;
	private ErrorCode errorCode;

	public static <T> CommonResult<T> fail(ErrorCode errorCode) {
		CommonResult<T> result = new CommonResult<>();
		result.setSuccess(false);
		if (errorCode != null) {
			result.setErrorCode(errorCode);
			result.setCode(errorCode.getCode());
			result.setMsg(errorCode.());
		}
		return result;
	}

	public static <T> CommonResult<T> fail(String code, String msg) {
		CommonResult<T> result = new CommonResult<>();
		result.setSuccess(false);
		result.setCode(code);
		result.setMsg(msg);
		return result;
	}

	public static <T> CommonResult<T> fail(String msg) {
		CommonResult<T> result = new CommonResult<>();
		result.setSuccess(false);
		result.setMsg(msg);
		return result;
	}

	public static <T> CommonResult<T> success(T data) {
		CommonResult<T> result = new CommonResult<T>();
		result.setSuccess(true);
		result.setData(data);
		return result;
	}

	public static <T> CommonResult<T> success(T data, String msg) {
		CommonResult<T> result = new CommonResult<T>();
		result.setSuccess(true);
		result.setData(data);
		result.setMsg(msg);
		return result;
	}

	public static <T> CommonResult<T> success(T data, String msg, String code) {
		CommonResult<T> result = new CommonResult<T>();
		result.setSuccess(true);
		result.setData(data);
		result.setMsg(msg);
		result.setCode(code);
		return result;
	}

	public T getData() {
		return data;
	}

	public CommonResult<T> setData(T data) {
		this.data = data;
		return this;
	}

	public boolean isSuccess() {
		return success;
	}

	public CommonResult<T> setSuccess(boolean success) {
		this.success = success;
		return this;
	}

	public String getCode() {
		return code;
	}

	public CommonResult<T> setCode(String code) {
		this.code = code;
		return this;
	}

	public String getMsg() {
		return msg;
	}

	public CommonResult<T> setMsg(String msg) {
		this.msg = msg;
		return this;
	}

	public ErrorCode getErrorCode() {
		return errorCode;
	}

	public CommonResult<T> setErrorCode(ErrorCode errorCode) {
		this.errorCode = errorCode;
		return this;
	}
}
